package com.xxx.yuedong.campaign.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaMode;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.json.JSONUtil;
import com.xxx.yuedong.campaign.controller.form.DeleteRunForm;
import com.xxx.yuedong.campaign.controller.form.InsertRunForm;
import com.xxx.yuedong.campaign.controller.form.SearchRunByIdForm;
import com.xxx.yuedong.campaign.controller.form.SearchRunListForm;
import com.xxx.yuedong.campaign.pojo.Run;
import com.xxx.yuedong.campaign.service.RunService;
import com.xxx.yuedong.common.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.HashMap;

/**
 * @author hc
 * @date 2023/08/03 11:54
 **/

@RestController
@RequestMapping("/run")
@Api(tags = "跑步相关接口")
@Slf4j
public class RunController {
    @Autowired
    private RunService runService;

    @PostMapping("/insert")
    @ApiOperation(value = "新增跑步记录")
    public R insert(@Valid @RequestBody InsertRunForm form){
        log.debug("正在执行 【新增跑步记录】,参数为:{}" + form.toString());
        Run run  = JSONUtil.parse(form).toBean(Run.class);
        run.setUserId(StpUtil.getLoginIdAsInt());
        int rows = runService.insert(run);
        return R.ok().put("rows",rows);
    }

    @PostMapping("/searchRunById")
    @ApiOperation(value = "根据ID查询跑步记录")
    public R searchRunById(@Valid @RequestBody SearchRunByIdForm form){
        log.debug("正在执行 【根据ID查询跑步记录】,参数为:{}" + form.toString());
        HashMap map = runService.searchRunById(form.getId());
        return R.ok(map);
    }

    @PostMapping("/searchRunList")
    @ApiOperation(value = "查询用户的跑步记录")
    public R searchRunList(@Valid @RequestBody SearchRunListForm form){
        log.debug("正在执行 【查询用户的跑步记录】,参数为:{}" + form.toString());
        ArrayList<HashMap> list = runService.selectRunList(form.getId());
        return R.ok().put("list",list);
    }

    @PostMapping("/delete")
    @ApiOperation(value = "删除用户的跑步记录")
    public R delete(@Valid @RequestBody DeleteRunForm form){
        log.debug("正在执行 【删除用户的跑步记录】,参数为:{}" + form.toString());
        int rows = runService.delete(form.getId());
        return R.ok().put("rows",rows);
    }
}
